package com.base.service.system.user;

import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.base.entity.Page;
import com.base.entity.system.User;
import com.base.mapper.UserMapper;
import com.base.util.PageData;

@Service("userService")
public class UserServiceImpl implements UserService {

	@Autowired
	private UserMapper userMapper;

	/**
	 * Validate user identity by selecting it from database
	 * 
	 * @param pd
	 * @return
	 * @throws Exception
	 */
	public boolean validateUser(PageData pageData) throws Exception {

		return userMapper.validateUser(pageData) == 1;

	}

	/**
	 * Confirm the existence of given user
	 * 
	 * @param pageData
	 * @return
	 * @throws Exception
	 */
	public Integer isUserExists(PageData pageData) throws Exception {

		return userMapper.isUserExists(pageData);
	}

	/**
	 * Confirm the availability of target EMAIL
	 * 
	 * @param pageData
	 * @return
	 * @throws Exception
	 */
	public Integer isEmailExists(PageData pageData) throws Exception {

		return userMapper.isEmailExists(pageData);
	}

	/**
	 * Confirm the availability of target phone number
	 * 
	 * @param pageData
	 * @return
	 * @throws Exception
	 */
	public Integer isPhoneExists(PageData pageData) throws Exception {

		return userMapper.isPhoneExists(pageData);
	}

	/**
	 * Confirm the validity of user STATUS
	 * 
	 * @param pageData
	 * @return
	 * @throws Exception
	 */
	public boolean isUserStatusActive(PageData pageData) throws Exception {
		return userMapper.isUserStatusActive(pageData);
	}

	/**
	 * Get user by ID
	 * 
	 * @param pd
	 * @return If target record does not exist, returns null; otherwise, returns
	 *         the record
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public HashMap<String, Object> getUserByUserName(PageData pd) throws Exception {
		return userMapper.selectUserByUserName(pd);
	}

	/**
	 * Get Name by ManBuyerID in DSR_DROPSHIPPER
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public HashMap<String, String> getNameByManBuyerID(PageData pageData) throws Exception {

		return userMapper.selectNameByManBuyerID(pageData);

	}

	/**
	 * Update LAST_LOGIN and IP
	 * 
	 * @throws Exception
	 */
	public boolean updateLoginInfo(PageData pd) throws Exception {

		return userMapper.updateLastLoginInfo(pd) == 1;
	}

	/**
	 * Insert new user into database
	 * 
	 * @param pd
	 * @throws Exception
	 */
	public boolean insertNewUser(PageData pd) throws Exception {
		return userMapper.insertNewUser(pd) == 1;
	}

	/**
	 * Insert new login flow record into "ULL_USER_LOGIN_LOGOUT_LOG"
	 * 
	 * @param pageData
	 * @return
	 * @throws Exception
	 */
	public boolean insertNewLoginRecord(PageData pageData) throws Exception {

		return userMapper.insertNewLoginRecord(pageData);
	}

	/**
	 * Get user by Email
	 * 
	 * @param pd
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public HashMap<String, String> selectUserByEmail(PageData pd) throws Exception {

		return userMapper.selectUserByEmail(pd);
	}

	/**
	 * Select ROLE_ID by USERNAME
	 * 
	 * @param pageData
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public HashMap<String, String> selectRoleNameByRoleID(PageData pageData) throws Exception {

		return userMapper.selectRoleNameByRoleID(pageData);
	}

	/**
	 * Update user information by user ID
	 * 
	 * @param pd
	 * @throws Exception
	 */
	public boolean updateUserInfoByUserName(PageData pd) throws Exception {

		return userMapper.updateUserInfo(pd);
	}

	/**
	 * Update MAN_BUYER_ID by UserName
	 * 
	 * @param pageData
	 * @return
	 * @throws Exception
	 */
	public boolean updateManByerIDByUserName(PageData pageData) throws Exception {

		return userMapper.updateManByerID(pageData);
	}

	/**
	 * Update UserName in database by for given USER_ID
	 * 
	 * @param pageData
	 * @return
	 * @throws Exception
	 */
	public boolean updateNickNameByUserName(PageData pageData) throws Exception {

		return userMapper.updateUserName(pageData);
	}

	/**
	 * Update user skin by USERNAME
	 * 
	 * @param pageData
	 * @return
	 * @throws Exception
	 */
	public boolean updateUserSkin(PageData pageData) throws Exception {

		return userMapper.updateUserSkin(pageData);
	}

	/**
	 * Update user EMAIL by USERNAME
	 * 
	 * @param pageData
	 * @return
	 * @throws Exception
	 */
	public boolean updateUserEmail(PageData pageData) throws Exception {

		return userMapper.updateUserEmail(pageData);
	}

	/**
	 * Update user NUMBER by USERNAME
	 * 
	 * @param pageData
	 * @return
	 * @throws Exception
	 */
	public boolean updateUserNumber(PageData pageData) throws Exception {

		return userMapper.updateUserNumber(pageData);
	}

	/**
	 * Update user PHONE by USERNAME
	 * 
	 * @param pageData
	 * @return
	 * @throws Exception
	 */
	public boolean updateUserPhone(PageData pageData) throws Exception {

		return userMapper.updateUserPhone(pageData);
	}

	/**
	 * Update use PASSWORD by USERNAME
	 * 
	 * @param pageData
	 * @return
	 * @throws Exception
	 */
	public boolean updateUserPassword(PageData pageData) throws Exception {

		return userMapper.updateUserPassword(pageData);
	}

	/*
	 * 通过id获取数据
	 */
	public PageData findByUiId(PageData pd) throws Exception {
		return userMapper.findByUiId(pd);
	}

	/*
	 * 通过loginname获取数据
	 */
	public PageData findByUId(PageData pd) throws Exception {
		return userMapper.findByUId(pd);
	}

	/*
	 * 通过邮箱获取数据
	 */
	public PageData findByUE(PageData pd) throws Exception {
		return userMapper.findByUE(pd);
	}

	/*
	 * 通过编号获取数据
	 */
	public PageData findByUN(PageData pd) throws Exception {
		return userMapper.findByUN(pd);
	}

	/*
	 * 保存用户
	 */
	public void saveU(PageData pd) throws Exception {
		userMapper.saveU(pd);
	}

	/*
	 * 修改用户
	 */
	public void editU(PageData pd) throws Exception {
		userMapper.editU(pd);
	}

	/*
	 * 换皮肤
	 */
	public void setSKIN(PageData pd) throws Exception {
		userMapper.setSKIN(pd);
	}

	/*
	 * 删除用户
	 */
	public void deleteU(PageData pd) throws Exception {
		userMapper.deleteU(pd);
	}

	/*
	 * 批量删除用户
	 */
	public void deleteAllU(String[] userIds) throws Exception {
		userMapper.deleteAllU(userIds);
	}

	/*
	 * 用户列表(用户组)
	 */
	public List<PageData> listPdPageUser(Page page) throws Exception {
		return userMapper.userlistPage(page);
	}

	/*
	 * 用户列表(全部)
	 */
	public List<PageData> listAllUser(PageData pd) throws Exception {
		return userMapper.listAllUser(pd);
	}

	/*
	 * 用户列表(供应商用户)
	 */
	public List<PageData> listGPdPageUser(Page page) throws Exception {
		return userMapper.userGlistPage(page);
	}

	/*
	 * 保存用户IP
	 */
	public void saveIP(PageData pd) throws Exception {
		userMapper.saveIP(pd);
	}

	/*
	 * 登录判断
	 */
	public PageData getUserByNameAndPwd(PageData pd) throws Exception {
		return userMapper.getUserInfo(pd);
	}

	/*
	 * 跟新登录时间
	 */
	public void updateLastLogin(PageData pd) throws Exception {
		userMapper.updateLastLogin(pd);
	}

	/*
	 * 通过id获取数据
	 */
	public User getUserAndRoleById(String userId) throws Exception {
		return userMapper.getUserAndRoleById(userId);
	}

}
